/*
 * Copyright (c) ycq_work@163.com
 */

package com.mxg.blog.controller.articleAdmin;

import com.mxg.blog.common.CqLogger;
// import com.mxg.blog.common.CreatToken;
import com.mxg.blog.common.RegularExpression;
import com.mxg.blog.common.ResultCode;
import com.mxg.blog.entity.User;
import com.mxg.blog.service.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: ycq_work@163.com
 * @DATE: 2023/5/8 21:23
 * 用以登录验证等
 */
// @CrossOrigin 跨域请求, 在debug时发现无法传送前段数据到后端,用这个解决跨域问题
@CrossOrigin
@Controller
public class Login {
    // 声明logger
    CqLogger logger = CqLogger.getInstance();
    // CreatToken creatToken = new CreatToken();

    // 装配UserService
    private final UserService userService;

    public Login(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("/api/login")
    public ResponseEntity<Map<String, Object>> login(@RequestBody Map<String, String> requestMap, HttpServletRequest request) {
        // 获取回传的信息
        // resultMap, 回传到前端的信息
        Map<String, Object> resultMap = new HashMap<>();
        // 前端传的账号  电话或邮箱
        String userAccount = requestMap.get("account");
        // 前端传的密码
        String userPassword = requestMap.get("password");
        // 前端传的验证码
        String checkCode = requestMap.get("checkCode");

        logger.print(requestMap);
        // session
        HttpSession session = request.getSession();
        // 获取session中验证码的值
        String captcha = (String) session.getAttribute("captcha");

        // 判断验证码是否正确, 错误返回错误信息
        if (!checkCode.equalsIgnoreCase(captcha)) {
            logger.print("验证码错误");
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_WRONG_CAPTCHA);
            resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_WRONG_CAPTCHA);
            return ResponseEntity.ok(resultMap);
        }

        // 正则判断
        RegularExpression regularExpression = new RegularExpression();
        User loginUser = new User();
        // 传入的是电话号码
        if (regularExpression.isPhoneNum(userAccount)) {
            // 通过电话查询用户信息
            loginUser = userService.getUserByUserPhoneNum(userAccount);
        }
        // 传入的是邮箱
        else if (regularExpression.isEmail(userAccount)){
            // 通过邮箱查询用户值
            loginUser = userService.getUserByUserEmail(userAccount);
        }

        // 找到的用户信息
        if(loginUser != null)
        {
            String password = loginUser.getUserPassword();
            // String md5Str = DigestUtils.md5DigestAsHex(password.getBytes());
            // 对比数据库信息和前端回传的密码是否一致
            if(userPassword.equals(password))
            {
                logger.print("登陆成功");
                // logger.print(md5Str);
                // 登录成功
                resultMap.put(ResultCode.code, ResultCode.SUCCESS_CODE);
                resultMap.put(ResultCode.message, ResultCode.SUCCESS_CODE);
                // String token = creatToken.createToken(loginUser);
                // resultMap.put("token", token);
                // logger.print(token);
            }
            else{
                resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_WRONG_PASSWORD);
                resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_WRONG_PASSWORD);
                logger.print("密码是:" + loginUser.getUserPassword());
            }
        }
        else{
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_WRONG_USER_ACCOUNT);
            resultMap.put("message", ResultCode.WRONG_MESSAGE_WRONG_USER_ACCOUNT);
            logger.print("不存在该用户");
        }

        return ResponseEntity.ok(resultMap);
    }

}
